/**
 * 版权所有(C)，上海勾芒信息科技，2016，所有权利保留。
 * 
 * 项目名：	car-web
 * 文件名：	MassMsgBuilder.java
 * 模块说明：	
 * 修改历史：
 * 2016年12月24日 - Debenson - 创建。
 */
package com.alex.wechat.mp.message.news.service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.web.cgform.common.CgAutoListConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alex.wechat.mp.config.WxConsts;
import com.alex.wechat.mp.message.mass.controller.MassMsgType;
import com.alex.wechat.mp.message.mass.entity.WxMassBillEntity;
import com.alex.wechat.mp.message.mass.service.WxMassBillServiceI;
import com.alex.wechat.mp.message.text.entity.WxTextTemplate;
import com.alex.wechat.mp.message.text.service.WxTextTemplateServiceI;
import com.alex.wechat.mp.util.TextTemplateTool;
import com.gomore.jeecg.common.Assert;

import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage;
import me.chanjar.weixin.mp.bean.WxMpMassTagMessage;

/**
 * 用来构造群发消息体。
 * 
 * @author Debenson
 * @since 0.1
 */
@Service
public class MassMsgBuilder {

  @Autowired
  private TextTemplateTool txtTplTool;
  @Autowired
  private WxTextTemplateServiceI textTplService;
  @Autowired
  private WxMassBillServiceI massBillService;

  public WxMpMassPreviewMessage preview(WxMassBillEntity bill, String toUserName) {

    WxMpMassPreviewMessage msg = new WxMpMassPreviewMessage();
    if (MassMsgType.CUSTOM_TEXT.equals(bill.getMsgType())) {
      // 自定义文本
      String rawTxt = bill.getTextMsg();
      msg.setMsgType(WxConsts.MASS_MSG_TEXT);
      msg.setContent(txtTplTool.resolve(rawTxt));

    } else if (MassMsgType.MASS_MSG_TEXT.equals(bill.getMsgType())) {
      // 文本模板
      WxTextTemplate tpl = textTplService.get(WxTextTemplate.class, bill.getTemplateId());
      if (tpl == null) {
        throw new BusinessException("找不到指定的文本消息模板");
      }
      String rawTxt = tpl.getContent();
      msg.setMsgType(WxConsts.MASS_MSG_TEXT);
      msg.setContent(txtTplTool.resolve(rawTxt));

    } else if (Arrays.asList(MassMsgType.MASS_MSG_IMAGE, MassMsgType.MASS_MSG_NEWS,
        MassMsgType.MASS_MSG_VIDEO, MassMsgType.MASS_MSG_VOICE).contains(bill.getMsgType())) {
      Assert.assertArgumentNotEmpty(bill.getMediaId(), "mediaId");

      msg.setMsgType(bill.getMsgType());
      msg.setMediaId(bill.getMediaId());

    } else {
      throw new BusinessException("不支持群发媒体类型：" + bill.getMsgType());
    }

    msg.setToWxUserName(toUserName);
    return msg;
  }

  public Serializable send(WxMassBillEntity bill) {
    if (CgAutoListConstant.BOOL_TRUE.equals(bill.getIsToAll())
        || StringUtils.isNotBlank(bill.getUserTagId())) {
      return sendTag(bill);
    } else {
      return sendOpenIds(bill);
    }
  }

  private WxMpMassTagMessage sendTag(WxMassBillEntity bill) {
    WxMpMassTagMessage msg = new WxMpMassTagMessage();

    if (MassMsgType.CUSTOM_TEXT.equals(bill.getMsgType())) {
      // 自定义文本
      String rawTxt = bill.getTextMsg();
      msg.setMsgType(WxConsts.MASS_MSG_TEXT);
      msg.setContent(txtTplTool.resolve(rawTxt));

    } else if (MassMsgType.MASS_MSG_TEXT.equals(bill.getMsgType())) {
      // 文本模板
      WxTextTemplate tpl = textTplService.get(WxTextTemplate.class, bill.getTemplateId());
      if (tpl == null) {
        throw new BusinessException("找不到指定的文本消息模板");
      }
      String rawTxt = tpl.getContent();
      msg.setMsgType(WxConsts.MASS_MSG_TEXT);
      msg.setContent(txtTplTool.resolve(rawTxt));

    } else if (Arrays.asList(MassMsgType.MASS_MSG_IMAGE, MassMsgType.MASS_MSG_NEWS,
        MassMsgType.MASS_MSG_VIDEO, MassMsgType.MASS_MSG_VOICE).contains(bill.getMsgType())) {
      Assert.assertArgumentNotEmpty(bill.getMediaId(), "mediaId");

      msg.setMsgType(bill.getMsgType());
      msg.setMediaId(bill.getMediaId());

    } else {
      throw new BusinessException("不支持群发媒体类型：" + bill.getMsgType());
    }

    if (CgAutoListConstant.BOOL_TRUE.equals(bill.getIsToAll())) {
      msg.setTagId(null);
    } else {
      msg.setTagId(Long.valueOf(bill.getUserTagId()));
    }
    return msg;
  }

  private WxMpMassOpenIdsMessage sendOpenIds(WxMassBillEntity bill) {
    WxMpMassOpenIdsMessage msg = new WxMpMassOpenIdsMessage();

    if (MassMsgType.CUSTOM_TEXT.equals(bill.getMsgType())) {
      // 自定义文本
      String rawTxt = bill.getTextMsg();
      msg.setMsgType(WxConsts.MASS_MSG_TEXT);
      msg.setContent(txtTplTool.resolve(rawTxt));

    } else if (MassMsgType.MASS_MSG_TEXT.equals(bill.getMsgType())) {
      // 文本模板
      WxTextTemplate tpl = textTplService.get(WxTextTemplate.class, bill.getTemplateId());
      if (tpl == null) {
        throw new BusinessException("找不到指定的文本消息模板");
      }
      String rawTxt = tpl.getContent();
      msg.setMsgType(WxConsts.MASS_MSG_TEXT);
      msg.setContent(txtTplTool.resolve(rawTxt));

    } else if (Arrays.asList(MassMsgType.MASS_MSG_IMAGE, MassMsgType.MASS_MSG_NEWS,
        MassMsgType.MASS_MSG_VIDEO, MassMsgType.MASS_MSG_VOICE).contains(bill.getMsgType())) {
      Assert.assertArgumentNotEmpty(bill.getMediaId(), "mediaId");

      msg.setMsgType(bill.getMsgType());
      msg.setMediaId(bill.getMediaId());

    } else {
      throw new BusinessException("不支持群发媒体类型：" + bill.getMsgType());
    }

    List<String> users = massBillService.getUsers(bill.getId());
    if (users.isEmpty()) {
      throw new BusinessException("没有设置微信用户列表");
    }
    msg.setToUsers(users);

    return msg;
  }
}
